package com.cmfi.service.impl;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.LineDao;
import com.cmfi.domain.Line;
import com.cmfi.service.inter.LineService;

@Service
public class LineServiceImpl implements LineService {
    @Autowired
    private LineDao lineDao;
    
    Logger log = LoggerFactory.getLogger(getClass());

    public boolean update(Line line, Long id) throws Exception {
    	line.setId(id);
        return lineDao.update(line) > 0;
    }

	@Override
	public long getTodayOutput() throws Exception {
		log.info("[LineServiceImpl][getTodayOutput] getting error.");
		List<Line> lineOutputList = lineDao.getTodayOutput();
		long lineOutput = lineOutputList.size();
		return lineOutput;
	}

	@Override
	public long getHistoryOutput(String beginningTime, String endTime) throws Exception {
		log.info("[LineServiceImpl][getHistoryOutput] getting error.");
		List<Line> historyOtputList = lineDao.getHistoryOutput(beginningTime, endTime);
		long historyOtput = historyOtputList.size();
		return historyOtput;
	}

	@Override
	public long getOperatingEfficiency() throws Exception {
		log.info("[LineServiceImpl][getOperatingEfficiency] getting error.");
		List<Line> operatingEfficiencyList = lineDao.getOperatingEfficiency();
		long operatingEfficiency = operatingEfficiencyList.size();
		return operatingEfficiency;
	}

	@Override
	public List<HashMap<Object, String>> getTodayRunningTime() throws Exception {
		log.info("[LineServiceImpl][getTodayRunningTime] getting error.");
		List<HashMap<Object, String>> todayRunningTimeList = lineDao.getTodayRunningTime();
		return todayRunningTimeList;
	}

	@Override
	public List<HashMap<Object, String>> getTotalRunningTime(String beginningTime, String endTime) throws Exception {
		log.info("[LineServiceImpl][getTotalRunningTime] getting error.");
		List<HashMap<Object, String>> totalRunningTimeList = lineDao.getTotalRunningTime(beginningTime, endTime);
		return totalRunningTimeList;
	}

	@Override
	public List<HashMap<Object, String>> getActivation(String beginningTime, String endTime) throws Exception {
		log.info("[LineServiceImpl][getActivation] getting error.");
		List<HashMap<Object,String>> totalRunningTimeList = lineDao.getTotalRunningTime(beginningTime, endTime);
		
		//开始时间和结束时间，格式“YYYY-MM-DD”
		LocalDate startDate = LocalDate.parse(beginningTime);
		LocalDate endDate = LocalDate.parse(endTime); 
		
		// 计算两个日期之间的总天数
        long totalDays = ChronoUnit.DAYS.between(startDate, endDate);
        
        // 将天数转换为小时（每天24小时）
        long totalHours = totalDays * 24;
        
        List<Long> totalRunningTimeForEachLineList = new ArrayList<>();
        for (HashMap<Object, String> map : totalRunningTimeList) {
        	Long runningTime = Long.parseLong(map.get("runningTime"));
            totalRunningTimeForEachLineList.add(runningTime);
        }
        
        //计算每条产线每天运行的小时数
        long actualHours1 = totalRunningTimeForEachLineList.get(0) / 3600;
        long actualHours2 = totalRunningTimeForEachLineList.get(1) / 3600;

		// 计算百分比
		double activation1 = ((double) actualHours1 / totalHours) * 100;
		double activation2 = ((double) actualHours2 / totalHours) * 100;
		
		List<HashMap<Object, String>> activationList = new ArrayList<>();
        HashMap<Object, String> map1 = new HashMap<>();
        map1.put("activation1", Double.toString(activation1));

        HashMap<Object, String> map2 = new HashMap<>();
        map2.put("activation2", Double.toString(activation2));

        activationList.add(map1);
        activationList.add(map2);
        
		return activationList;
	}

}
